package com.zh.auth.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zh.auth.domain.dto.AddPermissionDto;
import com.zh.auth.domain.dto.PagePermissionDto;
import com.zh.auth.domain.dto.UpdatePermissionDto;
import com.zh.auth.service.PermissionService;
import com.zh.common.annotation.NeedAuth;
import com.zh.common.annotation.NeedPermission;
import com.zh.common.annotation.NeedRoles;
import com.zh.common.annotation.Operator;
import com.zh.common.constant.AuthConstant;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import com.zh.web.base.PageApiResult;
import com.zh.web.domain.auth.entity.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
@Api(tags = "权限管理")
@Operator
@Validated
public class PermissionController extends BaseController {
    private final PermissionService permissionService;

    @ApiOperationSupport(order = 1)
    @PostMapping("/page")
    @ApiOperation(value = "分页查询权限")
    public ApiResult<PageApiResult<Permission>> pagePermission(@RequestBody PagePermissionDto pagePermissionDto) {
        return buildSuccessPackage(permissionService.pagePermission(pagePermissionDto));
    }

    @ApiOperationSupport(order = 2)
    @GetMapping("/{code}")
    @ApiOperation(value = "code查询权限")
    public ApiResult<Permission> selectPermissionByCode(@PathVariable Integer code) {
        return buildSuccessPackage(permissionService.getByCode(code));
    }

    @ApiOperationSupport(order = 3)
    @PostMapping("/add")
    @ApiOperation(value = "新增权限信息,需要管理员或者insert")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.INSERT)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> addPermission(@Valid @RequestBody AddPermissionDto addPermissionDto) {
        addPermissionDto.setCreateOperator(operator);
        addPermissionDto.setUpdateOperator(operator);
        return buildMsgPackage(permissionService.addPermission(addPermissionDto));
    }

    @ApiOperationSupport(order = 4)
    @PutMapping("/update")
    @ApiOperation(value = "修改权限信息,需要管理员或者update")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.UPDATE)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> updatePermission(@Valid @RequestBody UpdatePermissionDto updatePermissionDto) {
        updatePermissionDto.setUpdateOperator(operator);
        return buildMsgPackage(permissionService.updatePermission(updatePermissionDto));
    }

    @ApiOperationSupport(order = 5)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除权限信息,需要管理员或者delete")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.ADMIN)}, permissions = {@NeedPermission(value = AuthConstant.DELETE)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> deletePermission(@RequestBody List<Long> ids) {
        return buildMsgPackage(permissionService.deletePermission(ids));
    }
    @ApiOperationSupport(order = 6)
    @GetMapping("/getPermissionName2Code")
    @ApiOperation(value = "获取权限名称和code",hidden = true)
    public ApiResult<Map<String,Integer>> getPermissionName2Code(){
        return buildSuccessPackage(permissionService.getPermissionName2Code());
    }
}
